package com.airbnb.mvrx.sample.features.book.views

import android.content.Context
import android.support.v4.content.ContextCompat
import android.widget.FrameLayout
import android.widget.TextView
import com.airbnb.epoxy.ModelProp
import com.airbnb.epoxy.ModelView
import com.airbnb.mvrx.sample.Utils
import com.airbnb.mvrx.sample.features.book.models.PriceModel
import com.example.mvrxstudy.R
import com.facebook.drawee.view.SimpleDraweeView
import com.yt.ytdeep.client.dto.LessonDTO

@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT)
class BDRecommendLesson @JvmOverloads constructor(context: Context) : FrameLayout(context) {

    private val mImage: SimpleDraweeView by lazy { findViewById<SimpleDraweeView>(R.id.iv_image) }
    private val mTitle: TextView by lazy { findViewById<TextView>(R.id.tv_title) }
    private val mContent: TextView by lazy { findViewById<TextView>(R.id.tv_content) }
    private val mPrice: TextView by lazy { findViewById<TextView>(R.id.tv_price) }
    private val mLearned: TextView by lazy { findViewById<TextView>(R.id.tv_learned) }


    init {
        inflate(context, R.layout.view_lesson_recommend, this)
    }

    @ModelProp
    fun setImage(image: String) {
        mImage.setImageURI(image)
    }

    @ModelProp
    fun setTitle(title: String) {
        mTitle.text = title
    }

    @ModelProp
    fun setContent(content: String) {
        mContent.text = content
    }

    @ModelProp
    fun setPrice(priceModel: PriceModel) {
        when (priceModel.authType) {
            LessonDTO.LESSON_AUTHTYPE_NEED_PRICE ->
                mPrice.text = Utils.format("￥%.2f", priceModel.authVal!!.toFloat() / 100)
            LessonDTO.LESSON_AUTHTYPE_FREE -> {
                mPrice.setTextColor(ContextCompat.getColor(context, R.color.green))
                mPrice.text = "免费"
            }
            LessonDTO.LESSON_AUTHTYPE_NEED_LEARN_CARD -> {
                mPrice.setTextColor(ContextCompat.getColor(context, R.color.blue))
                mPrice.text = "学习卡"
            }
        }
    }

    @ModelProp
    fun setLearned(learned: Int) {
        mLearned.text = Utils.format("%d人已学", learned)
    }

}